<?php

namespace app\api\controller;


use app\api\biz\MenuBiz;
use app\common\controller\ApiController;
use app\common\Result;
use joncc\attribute\attr\AuthNeedless;
use joncc\attribute\attr\Comment;
use joncc\attribute\attr\Params;
use joncc\attribute\attr\PermissionDefine;
use joncc\attribute\attr\PermissionNeedless;
use think\response\Json;


#[Comment('菜单')]
#[AuthNeedless]
#[PermissionNeedless]
#[PermissionDefine('list', '树列表')]
#[PermissionDefine('add', '新增')]
#[PermissionDefine('edit', '编辑')]
#[PermissionDefine('del', '删除')]
class Menu extends ApiController
{

    #[Params('all', Params::BOOL, false, "是否全部")]
    public function list(): Json
    {
        $all = p('all');
        $all = is_null($all) ? true : !!$all;
        $menuBiz = MenuBiz::list([], $all);
        return Result::success($menuBiz::getData());
    }

    #[Params('name', Params::STRING, true, "名称")]
    #[Params('type', Params::INT, false, "1目录 2菜单 3按钮")]
    #[Params('pid', Params::INT, false, "父级菜单ID(最高级0)")]
    #[Params('icon', Params::STRING, false, "图标")]
    #[Params('sort', Params::INT, false, "排序")]
    #[Params('paths', Params::STRING, false, "路由地址")]
    #[Params('perms', Params::STRING, false, "权限标识")]
    #[Params('component', Params::STRING, false, "前端组件")]
    #[Params('params', Params::STRING, false, "路由参数")]
    #[Params('is_show', Params::BOOL, false, "显示")]
    #[Params('is_disable', Params::BOOL, false, "禁用")]
    #[Params('memo', Params::STRING, false, "备注")]
    public function add(): Json
    {
        $menuBiz = MenuBiz::add(p());
        if ($menuBiz::hasError()) {
            return Result::fail(...$menuBiz::getError());
        }
        return Result::success($menuBiz::getData());
    }

    #[Params('id', Params::INT, true, "ID")]
    #[Params('name', Params::STRING, true, "名称")]
    #[Params('type', Params::INT, false, "1目录 2菜单 3按钮")]
    #[Params('pid', Params::INT, false, "父级菜单ID(最高级0)")]
    #[Params('icon', Params::STRING, false, "图标")]
    #[Params('sort', Params::INT, false, "排序")]
    #[Params('paths', Params::STRING, false, "路由地址")]
    #[Params('perms', Params::STRING, false, "权限标识")]
    #[Params('component', Params::STRING, false, "前端组件")]
    #[Params('params', Params::STRING, false, "路由参数")]
    #[Params('is_show', Params::BOOL, false, "显示")]
    #[Params('is_disable', Params::BOOL, false, "禁用")]
    #[Params('memo', Params::STRING, false, "备注")]
    public function edit(): Json
    {
        $menuBiz = MenuBiz::edit(p());
        if ($menuBiz::hasError()) {
            return Result::fail(...$menuBiz::getError());
        }
        return Result::success($menuBiz::getData());
    }

    #[Params('ids', Params::STRING, true, "ids多个','分隔")]
    public function del(): Json
    {
        $menuBiz = MenuBiz::del(p());
        if ($menuBiz::hasError()) {
            return Result::fail(...$menuBiz::getError());
        }
        return Result::success($menuBiz::getData());
    }
}
